﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OrdersDB.Models;

namespace OrdersDB.Controllers
{
    public class OrderInfoesController : Controller
    {
        private OrdersDBEntities db = new OrdersDBEntities();

        // GET: OrderInfoes
        public ActionResult Index(string key)
        {
            var list = db.OrderInfo.Where(order => order.UserName.Contains(key))
                .OrderBy(o => o.OrderState).ToList();
            ViewBag.Key = key;
            return View(list);
        }

        // GET: OrderInfoes/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderInfo = db.OrderInfo.Find(id);
            if (orderInfo == null)
            {
                return HttpNotFound();
            }
            return View(orderInfo);
        }

        // GET: OrderInfoes/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: OrderInfoes/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "OrderID,UserName,OrderDate,OrderState")] OrderInfo orderInfo)
        {
            if (ModelState.IsValid)
            {
                orderInfo.OrderState = 0;
                db.OrderInfo.Add(orderInfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(orderInfo);
        }

        public ActionResult SendOrder(int id)
        {
            var order = db.OrderInfo.Find(id);
            order.OrderState = 1;
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        // GET: OrderInfoes/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderInfo = db.OrderInfo.Find(id);
            if (orderInfo == null)
            {
                return HttpNotFound();
            }
            return View(orderInfo);
        }

        // POST: OrderInfoes/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "OrderID,UserName,OrderDate,OrderState")] OrderInfo orderInfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(orderInfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(orderInfo);
        }

        // GET: OrderInfoes/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderInfo orderInfo = db.OrderInfo.Find(id);
            if (orderInfo == null)
            {
                return HttpNotFound();
            }
            return View(orderInfo);
        }

        // POST: OrderInfoes/Delete/5
        
       
        public ActionResult DeleteConfirmed(int id)
        {
            OrderInfo orderInfo = db.OrderInfo.Find(id);
            db.OrderInfo.Remove(orderInfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
